// pages/addBox/addPicture.js
var app = getApp();
var common = require('../../../service/common.js');
var user = require('../../../service/user.js');
var filePath = [];
var page_id = -1;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    filePath: [],
    index: -1,
    edit: -1,
    current: 0,
    DataList: [],
    types: 1,
    token:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    if (options.type == 2) {
      this.setData({
        DataList: wx.getStorageSync("DataList"),
        index: options.index,
        types: options.type,
        token: user.getTk(app.globalData.userInfo.openid)
      })
      console.log(this.data.DataList)
    } else {

    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    filePath = [];
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /*添加上传图片*/
  addPicture: function () {
    var that = this;
    wx.chooseImage({
      count: 9 - filePath.length, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        for (var i = 0; i < tempFilePaths.length; i++) {
          var temp = tempFilePaths[i];
            wx.uploadFile({
              url: common.getUrl('website.uploadImg'),
              filePath: temp,
              name: 'file',
              formData: {
                token: that.data.token,
              },
              success: function (ret) {
                var res = JSON.parse(ret.data);
                if (res.code == 0) {
                  var filePath = that.data.filePath;
                  filePath.push(res.data.url)
                  that.setData({
                    filePath: filePath
                  })
                } else {
                  wx.showModal({
                    title: '提示',
                    content: res.message,
                  })
                }
              },
              fail: function (ret) {
                var res = JSON.parse(ret.data);
                wx.showToast({
                  title: res.message,
                })
              }
            })

          
        }

      }
    })
  },


  /**删除对应的图片 */
  deleteImg: function (e) {
    var deleteIndex = e.currentTarget.dataset.index;
    var filePath = this.data.filePath;
    filePath.splice(deleteIndex, 1);
    this.setData({
      filePath: filePath
    })
  },


  /**轮播图切换时触发 */
  swiperChange: function (e) {
    this.setData({
      current: e.detail.current
    })
  },


  /**点击图片时触发 */
  tabImage: function (e) {
    var current = e.currentTarget.dataset.index;
    this.setData({
      current: current
    })
  },


  /*提交当前内容*/
  savePicture: function (e) {
    if (this.data.filePath.length == 0) {
      wx.showToast({
        image: "/image/wrong.png",
        title: '请上传至少一张图片',
      })
      return
    } else {
      if (this.data.types == 2) {
        var data = {
          lb_type: 4,
          types: 3,
          content: this.data.filePath
        }
        this.data.DataList.splice(this.data.index, 0, data)
      }
      wx.setStorageSync("DataList", this.data.DataList)
      setTimeout(() => {
        wx.navigateBack()
      }, 500)
    }

  }
})